<?php
namespace Home\Model;
use Think\Model;

class CartModel extends Model
{
    //购物车信息存放到本地cookie
    public function Add()
    {
        //create=====>>根据表单提交的POST数据创建数据对象
        //检测是否登陆
        $uid = session('uid');
        sort($this->goods_attr_id,SORT_NUMERIC);
        $this->goods_attr_id = implode(',',$this->goods_attr_id);
        if(!$uid){
            //未登录,检测购物车cookie是否存在,存在->反序列化为数组;不存在->创建为新数组
            $cart = isset($_COOKIE['cart'])?unserialize($_COOKIE['cart']):array();
            //信息存放(用一维数组('商品ID-商品属性id'=>goods_number))
            //拼接cookie信息存放格式
            $key = $this->gid.'-'.$this->goods_attr_id;
            //存入格式(添加商品数量)
            if(isset($cart[$key])){
                $cart[$key] += $this->goods_number;
            }else{
                $cart[$key] = $this->goods_number;
            }
            //将一维数组存入到cookie中
            setcookie('cart',serialize($cart),time()+30*24*3600,'/');
//          var_dump($_COOKIE);die();
            return true;
        }else{
            //购物车信息存到数据库
            //数据库中找该商品
            $goods_number = $this->goods_number;
            $model = D('cart');
            $has = $model->field('id')->where(array(
                'uid' => $uid,
                'gid' => $this->gid,
               // 'goods_attr_id' => $this->goods_attr_id,
            ))->find();

            if($has){
                $this->where(array(
                    'id'=>array('eq',$has['id'])
                ))->setInc('goods_number',$goods_number);
                return true;
            }else{
                $model = D('cart');
                $model->uid = $uid;
                $model->gid = $this->gid;
                $model->goods_attr_id = $this->goods_attr_id;
                $model->goods_number = $this->goods_number;
                if(parent::add()){
                    return true;
                }else{
                    return false;
                }
            }
        }
    }

    //用户登录后购物车信息存到数据库
    public function cookietoDB(){
        //检测登录状态
        $uid = session('uid');
        if($uid){
            $cart = isset($_COOKIE['cart'])?unserialize($_COOKIE['cart']):array();//$cart=>一位数组
            //拆分一维数组-.-
            $goods_nunmber = $this->goods_number;
            foreach($cart as $k => $v){
                $_k = explode(',',$k);
                $has = $this->field('id')->where(array(
                    'gid'=>$_k[0],
                    'goods_attr_id'=>$_k[1],
                    'uid'=>$uid,
                ))->find();
            }
            if($has){
                $this->where(array(
                    'id'=>array('eq',$has['id']),
                ))->setInc('goods_number',$v);
            }else{
                parent::add(array(
                    'gid'=> $k[0],
                    'goods_attr_id'=> $_k[1],
                    'goods_number' => $v,
                    'uid' => $uid,
                ));
            }
        }
        //clear cookie
        setcookie('cart','',time()-1,'/');
    }

    public function checkout(){
        //var_dump($_COOKIE);die();
        //var_dump(session());
        return $this->alias('a')
                    ->join('left join marry_goods b on a.gid=b.gid')
                    ->where(array(
                        'a.uid'=>array('eq',session('uid')),
                    ))->select();

    }
}

?>